package com.example.osstest.service;

import com.example.osstest.config.LocalConfig;
import com.example.osstest.vo.FileUploadResult;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
public class LocalFileUploadService {

    @Autowired
    private LocalConfig localConfig;

    /**
     * @desc 文件上传
     * @param files
     * @return
     */

    public FileUploadResult[] upload(HttpServletRequest request, MultipartFile[] files)throws IllegalStateException,IOException {

        FileUploadResult[] fileUploadResult = new FileUploadResult[files.length];
        //构建文件上传路径
//        String destionation = localConfig.getUploadPath() +File.separator +"upload";
        String destionation = localConfig.getUploadPath();
        System.out.println("destionation="+destionation);
        for(int i = 0;i<files.length;i++){
            fileUploadResult[i]=new FileUploadResult();
            //如果文件不为空，写入上传路径，进行文件上传
            if(null != files && files.length>0){
                System.out.println("文件类型ContentType=" + files[i].getContentType());
                System.out.println("文件组件名称Name=" + files[i].getName());
                System.out.println("文件原名称OriginalFileName=" + files[i].getOriginalFilename());
                System.out.println("文件大小Size=" + files[i].getSize() + "byte or " + files[i].getSize()/1024 + "KB");
                String visitUrl = saveFile(request,files[i], destionation);
                fileUploadResult[i].setStatus("done");
                fileUploadResult[i].setResponse("success");
                //this.aliyunConfig.getUriPrefix()+fileUrl文件路径需要保存到数据库
                fileUploadResult[i].setName(visitUrl);
                //System.currentTimeMillis()获得当前时间，String.valueOf()是将当前时间转换成字符串
                fileUploadResult[i].setUid(String.valueOf(System.currentTimeMillis()));
            }else {
                fileUploadResult[i].setStatus("error");
            }
        }
        return fileUploadResult;
    }
    private String saveFile(HttpServletRequest request,MultipartFile file, String destination)throws IllegalStateException,IOException
    {
        //获取上传的文件名称，并结合存放路径，构建新的文件名称

        String origFileName = file.getOriginalFilename();
        String fileName = DateUtils.formatDate(new Date(),"yyyy-MM-dd")+ "-"+origFileName;
        File filePath = new File(destination,fileName);
        //判断路径是否存在，不存在就新创建一个
        if(!filePath.getParentFile().exists()){
            //获得父目录，mkdirs()是生成目录文件夹
            filePath.getParentFile().mkdirs();
        }
        //将上传文件保存到目标文件目录
        file.transferTo(new File(destination + File.separator + fileName));
        String visitUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/local/"+fileName;
        return visitUrl;
    }
}
